package com.sann.spring.dao.impl;

import com.sann.spring.beans.Account;
import com.sann.spring.dao.IAccountDao;
import com.sann.spring.util.ConnectionUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Dao层里面的QueryRunner 如果在没有给其配置Connection对象时，不会从连接池中拿连接，而是在调用
 * QueryRunner#query(Connction)时指定Connection对象，这个对象是我们通过ConnectionUtil 提供的与线程绑定的连接。
 *
 * 如果该项目案例演示的是beans.xml配置文件方式配置，那么就要将
 * <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
 *      <constructor-arg name="dataSource" ref="dataSource"/>
 * </bean>
 * 改为
 * <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"/>
 * 这样QueryRunner 就不会从连接池中拿连接，而是通过query()方法调用时，传递Connection连接进行和数据库通信。
 */
@Repository(value="accountDao")
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    @Qualifier("runner")
    private QueryRunner runner;

    @Autowired
    @Qualifier("connectionUtil")
    private ConnectionUtil connectionUtil;

    @Override
    public Account findByName(String accountName) {
        try {
            List<Account> accounts = runner.query(connectionUtil.getThreadConnection(),
                    "select * from tb_account where name=?",
                    new BeanListHandler<Account>(Account.class), accountName);
            // 不存在该账户
            if(accounts==null || accounts.size()==0){
                return null;
            }
            // 存在多个账户
            if(accounts.size()>1){
                throw new RuntimeException("账户不唯一");
            }
            // 存在唯一账户，并返回该唯一账户
            return accounts.get(0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Account> findAllAccount() {
        try {
            return runner.query(connectionUtil.getThreadConnection(),
                    "select * from tb_account",
                    new BeanListHandler<Account>(Account.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Account findAccountById(Integer accountId) {
        try {
            return runner.query(connectionUtil.getThreadConnection(),
                    "select * from tb_account where id=?",
                    new BeanHandler<>(Account.class),accountId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void saveAccount(Account account) {
        try {
            runner.update(connectionUtil.getThreadConnection(),
                    "insert into tb_account(name, money)values(?,?)",
                    account.getName(),account.getMoney());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateAccount(Account account) {
        try {
            runner.update(connectionUtil.getThreadConnection(),
                    "update tb_account set name=?, money=? where id=?",
                    account.getName(),account.getMoney(),account.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteAccount(Integer accountId) {
        try {
            runner.update(connectionUtil.getThreadConnection(),
                    "delete from tb_account where id=?", accountId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
